
###############################################################################
### Did 3G Make Afghan Insurgents Fight More Effectively? 
### Replication files for analyses
### Mehmet Erdem Arslan
###
### Analyses
### Spatial Regressions (Main Text and the Online Appendix)
###############################################################################

# setwd("your file path here")

library(splm)
library(plm)

source("R/Functions.R")

load("R/Spatial_regressions.Rdata")
# w: neighbour list for spatial analyses

set.seed(1234)

####### MAIN TEXT #######

##### Table 3 #####

#### Dependent Variable 2 = GTD multiple coordinated attacks ####

# Model 1: Two-way FE
add_twfe_dv2 <- plm(log(crd+1) ~ lag.two + lag.three + lag.troops, data = data, 
                    effect = "twoways", model = "within", index = c("id", "time"))

# Model 2: General nesting model with time lag (takes a few minutes)
add_gnmtl_dv2 <- spml(log(crd+1) ~ lag.two + lag.three + lag.troops + log(plm::lag(crd, 1) + 1) + 
                      Wtwo + Wthree, data = data, index = c("id", "time"), 
                      listw=w, lag=TRUE, spatial.error="b", model="pooling")
aic_splm(add_gnmtl_dv2)

# Model 3: General nesting model with TWFE
add_gnmfe_dv2 <- spml(log(crd+1) ~ lag.two + lag.three + lag.troops + Wtwo + Wthree, 
                      data = data, index =c("id", "time"), listw=w, lag = TRUE, 
                      spatial.error = "b", zero.policy = TRUE, model = "within",
                      effect = "twoways", quiet = TRUE)

# Interactions with troop presence variable #
# Model 4: Two-way FE
twfe_dv2 <- plm(log(crd+1) ~ lag.two*lag.troops + lag.three*lag.troops, 
                data = data, effect = "twoways", model = "within", index = c("id", "time"))

# Model 5: General nesting model with time lag (takes a few minutes)
gnmtl_dv2 <- spml(log(crd+1) ~ lag.two*lag.troops + lag.three*lag.troops + 
                    log(plm::lag(crd, 1) + 1) + Wtwo + Wthree, data = data, index =c("id", "time"),
                  listw=w, lag=TRUE, spatial.error="b", model="pooling")
aic_splm(gnmtl_dv2)

# Model 6: General nesting model with TWFE 
gnmfe_dv2 <- spml(log(crd+1) ~ lag.two*lag.troops + lag.three*lag.troops + 
                    Wtwo + Wthree, data = data, index = c("id", "time"), listw=w, 
                  lag = TRUE, spatial.error = "b", zero.policy = TRUE, model = "within",
                  effect = "twoways", quiet = TRUE)

coef.table(models = list(add_twfe_dv2, add_gnmtl_dv2, add_gnmfe_dv2,
                         twfe_dv2, gnmtl_dv2, gnmfe_dv2))


####### ONLINE APPENDICES #######

##### Appendix B: Regression Results using GTD Events #####

##### Table 5 #####

#### Dependent Variable 1 = GTD events ####

# Model 1: Two-way FE
add_twfe_dv1 <- plm(log(all+1) ~ lag.two + lag.three + lag.troops, data = data, 
                    effect = "twoways", model = "within", index = c("id", "time"))

# Model 2: General nesting model with time lag (takes a few minutes)
add_gnmtl_dv1 <- spml(log(all+1) ~ lag.two + lag.three + lag.troops + 
                        log(plm::lag(all, 1) + 1) + Wtwo + Wthree, data = data, index =c("id", "time"),
                      listw=w, lag=TRUE, spatial.error="b", model="pooling")
aic_splm(add_gnmtl_dv1)

# Model 3: General nesting model with TWFE
add_gnmfe_dv1 <- spml(log(all+1) ~ lag.two + lag.three + lag.troops + Wtwo + Wthree, 
                      data = data, index = c("id", "time"), listw=w, lag = TRUE, 
                      spatial.error = "b", zero.policy = TRUE, model = "within",
                      effect = "twoways", quiet = TRUE)

# Interactions with troop presence variable #
# Model 4: Two-way FE
twfe_dv1 <- plm(log(all+1) ~ lag.two*lag.troops + lag.three*lag.troops, 
                data = data, effect = "twoways", model = "within", index = c("id", "time"))

# Model 5: General nesting model with time lag (takes a few minutes)
gnmtl_dv1 <- spml(log(all+1) ~ lag.two*lag.troops + lag.three*lag.troops + 
                  log(plm::lag(all, 1) + 1) + Wtwo + Wthree, data = data, index =c("id", "time"),
                  listw=w, lag=TRUE, spatial.error="b", model="pooling")
aic_splm(gnmtl_dv1)

# Model 6: General nesting model with TWFE 
gnmfe_dv1 <- spml(log(all+1) ~ lag.two*lag.troops + lag.three*lag.troops + 
                  Wtwo + Wthree, data = data, index = c("id", "time"), listw=w, 
                  lag = TRUE, spatial.error = "b", zero.policy = TRUE, model = "within",
                  effect = "twoways", quiet = TRUE)

coef.table(models = list(add_twfe_dv1, add_gnmtl_dv1, add_gnmfe_dv1,
                         twfe_dv1, gnmtl_dv1, gnmfe_dv1))


##### Table 6 #####

#### Dependent Variable 3 = IED attacks ####

# Model 1: Two-way FE
add_twfe_ied <- plm(log(ied+1) ~ lag.two + lag.three + lag.troops, data = data, 
                    effect = "twoways", model = "within", index = c("id", "time"))

# Model 2: General nesting model with time lag (takes a few minutes)
add_gnmtl_ied <- spml(log(ied+1) ~ lag.two + lag.three + lag.troops + log(plm::lag(ied, 1) + 1) + 
                        Wtwo + Wthree, data = data, index =c("id", "time"),
                      listw=w, lag=TRUE, spatial.error="b", model="pooling")
aic_splm(add_gnmtl_ied)

# Model 3: General nesting model with TWFE
add_gnmfe_ied <- spml(log(ied+1) ~ lag.two + lag.three + lag.troops + Wtwo + Wthree, 
                      data = data, index =c("id", "time"), listw=w, lag = TRUE, 
                      spatial.error = "b", zero.policy = TRUE, model = "within",
                      effect = "twoways", quiet = TRUE)

# Interactions with troop presence variable #
# Model 4: Two-way FE
twfe_ied <- plm(log(ied+1) ~ lag.two*lag.troops + lag.three*lag.troops,
                data = data, effect = "twoways", model = "within", index = c("id", "time"))

# Model 5: General nesting model with time lag (takes a few minutes)
gnmtl_ied <- spml(log(ied+1) ~ lag.two*lag.troops + lag.three*lag.troops + 
                  log(plm::lag(ied, 1) + 1) + Wtwo + Wthree, data = data, index =c("id", "time"),
                  listw=w, lag=TRUE, spatial.error="b", model="pooling")
aic_splm(gnmtl_ied)

# Model 6: General nesting model with TWFE 
gnmfe_ied <- spml(log(ied+1) ~ lag.two*lag.troops + lag.three*lag.troops + 
                  Wtwo + Wthree, data = data, index = c("id", "time"), listw=w, 
                  lag = TRUE, spatial.error = "b", zero.policy = TRUE, model = "within",
                  effect = "twoways", quiet = TRUE)

coef.table(models = list(add_twfe_ied, add_gnmtl_ied, add_gnmfe_ied,
                         twfe_ied, gnmtl_ied, gnmfe_ied))


##### Appendix G: Regression results with the alternative definition of multiple coordinated attacks #####

##### Table 13 #####

#### Dependent Variable = Alternative definition of coordinated attacks ####

# Model 1: Two-way FE
add_twfe_alt <- plm(log(alt+1) ~ lag.two + lag.three + lag.troops, data = data, 
                    effect = "twoways", model = "within", index = c("id", "time"))

# Model 2: General nesting model with time lag (takes a few minutes)
add_gnmtl_alt <- spml(log(alt+1) ~ lag.two + lag.three + lag.troops + log(plm::lag(alt, 1) + 1) + 
                      Wtwo + Wthree, data = data, index =c("id", "time"), listw=w, 
                      lag=TRUE, spatial.error="b", zero.policy = TRUE, model="pooling")
aic_splm(add_gnmtl_alt)

# Model 3: General nesting model with TWFE
add_gnmfe_alt <- spml(log(alt+1) ~ lag.two + lag.three + lag.troops + Wtwo + Wthree, 
                      data = data, index = c("id", "time"), listw=w, lag = TRUE, 
                      spatial.error = "b", zero.policy = TRUE, model = "within",
                      effect = "twoways", quiet = TRUE)

# Interactions with troop presence variable #
# Model 4: Two-way FE
twfe_alt <- plm(log(alt+1) ~ lag.two*lag.troops + lag.three*lag.troops, 
                data = data, effect = "twoways", model = "within", index = c("id", "time"))

# Model 5: General nesting model with time lag (takes a few minutes)
gnmtl_alt <- spml(log(alt+1) ~ lag.two*lag.troops + lag.three*lag.troops + 
                  log(plm::lag(alt, 1) + 1) + Wtwo + Wthree, data = data, index =c("id", "time"),
                  listw=w, lag=TRUE, spatial.error="b", model="pooling") 
aic_splm(gnmtl_alt)

# Model 6: General nesting model with TWFE 
gnmfe_alt <- spml(log(alt+1) ~ lag.two*lag.troops + lag.three*lag.troops + 
                  Wtwo + Wthree, data = data, index = c("id", "time"), listw=w, 
                  lag = TRUE, spatial.error = "b", zero.policy = TRUE, model = "within",
                  effect = "twoways", quiet = TRUE)

coef.table(models = list(add_twfe_alt, add_gnmtl_alt, add_gnmfe_alt,
                         twfe_alt, gnmtl_alt, gnmfe_alt))


##### Appendix H: Regression results with binary values of 2G and 3G coverage #####

##### Table 14 #####

#### Binary specifications of Independent Variables --2G and 3G-- ####

# Model 1: Two-way FE
add_twfe_bIV <- plm(log(all+1) ~ lag.two.bin + lag.three.bin + lag.troops, data = data, 
                    effect = "twoways", model = "within", index = c("id", "time"))

# Model 2: General nesting model with time lag (takes a few minutes)
add_gnmtl_bIV <- spml(log(all+1) ~ lag.two.bin + lag.three.bin + lag.troops + 
                      log(plm::lag(all, 1) + 1) + Wtwo.bin + Wthree.bin, data = data, 
                      index = c("id", "time"), listw=w, lag=TRUE, spatial.error="b", 
                      model="pooling")
aic_splm(add_gnmtl_bIV)

# Model 3: General nesting model with TWFE
add_gnmfe_bIV <- spml(log(all+1) ~ lag.two.bin + lag.three.bin + lag.troops + 
                      Wtwo.bin + Wthree.bin, data = data, index = c("id", "time"), 
                      listw=w, lag = TRUE, spatial.error = "b", zero.policy = TRUE, 
                      model = "within", effect = "twoways", quiet = TRUE)

# Interactions with troop presence variable #
# Model 4: Two-way FE
twfe_bIV <- plm(log(all+1) ~ lag.two.bin*lag.troops + lag.three.bin*lag.troops, 
                data = data, effect = "twoways", model = "within", index = c("id", "time"))

# Model 5: General nesting model with time lag (takes a few minutes)
gnmtl_bIV <- spml(log(all+1) ~ lag.two.bin*lag.troops + lag.three.bin*lag.troops + 
                  log(plm::lag(all, 1) + 1) + Wtwo.bin + Wthree.bin, data = data, index =c("id", "time"),
                  listw=w, lag=TRUE, spatial.error="b", model="pooling")
aic_splm(gnmtl_bIV)

# Model 6: General nesting model with TWFE 
gnmfe_bIV <- spml(log(all+1) ~ lag.two.bin*lag.troops + lag.three.bin*lag.troops + 
                  Wtwo.bin + Wthree.bin, data = data, index = c("id", "time"), 
                  listw=w, lag = TRUE, spatial.error = "b", zero.policy = TRUE, model = "within",
                  effect = "twoways", quiet = TRUE)


coef.table(models = list(add_twfe_bIV, add_gnmtl_bIV, add_gnmfe_bIV,
                         twfe_bIV, gnmtl_bIV, gnmfe_bIV), bin = TRUE)


rm(list = ls())

###### end of script ######

